package com.huifaapp.mall.uitils;

import android.content.Context;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.afinal.Constants;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.api.ObserverCallback;
import com.huifaapp.mall.bean.WXSignatureBean;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;



public class WXPayUtil {
	private Context context;
	private IWXAPI api;
	public WXPayUtil(Context context) {
		this.context = context;
		api = WXAPIFactory.createWXAPI(context, null);
		if (api.isWXAppInstalled()) {
			api.registerApp(Constants.WX_APPID);
//			queryPay( context, ryid,  money,  type);
		} else {
			 ToastUtils.showShortSafe("没有安装微信，请稍后");
		}

	}




	/**
	 * 微信签名VIP
	 */
	@SuppressWarnings("unused")
	public void getWXpaySignature(String ryid, String money, String type) {

        String token= SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN);
        ApiManager.getInstance()
                .getApiServer()
                .getWXPaySign("",token)
                .compose(ApiManager.<String>handlResult())
                .subscribe(new ObserverCallback<String>(context,"获取信息中") {
                    @Override
                    protected void onSuccessful(String resp) {
                        WXSignatureBean signatureBean = new Gson().fromJson(resp, WXSignatureBean.class);
                        PayReq req = new PayReq();
                        req.appId = Constants.WX_APPID;
                        req.nonceStr = signatureBean.getNoncestr();
                        req.partnerId = signatureBean.getPartnerid();
                        req.timeStamp = signatureBean.getTimestamp();
                        req.sign = signatureBean.getPaysign();
                        req.prepayId = signatureBean.getPrepayid();
                        req.packageValue = signatureBean.getPackagee();
//					    req.extData = ""+ Constant.PAY_VIP; // // 设置VIP支付标记
                        api.sendReq(req);
                    }

                    @Override
                    protected void onError(String code, String errMsg) {

                        ToastUtils.showShortSafe(errMsg);

                    }
                });


	}


}
